package com.cn.psys.jhfb.imp;


import com.cn.psys.system.entity.JhfbStu;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import java.io.File;
import java.math.BigDecimal;
import java.util.*;

public class Jhfbbak {



    /**
     * 根据条件自动编班
     * @param className
     * @param class_num
     * @param sexFlag
     * @param filePath
     * @param proId
     * @return
     */
    public static Map<String,List<JhfbStu>> dotask(String className, int class_num, String sexFlag, String filePath){
            // 学生集合,list_stu_w男生，list_stu_m女生
            List<JhfbStu> list_stu_w = null;
            List<JhfbStu> list_stu_m = null;
            // 学生对象
            JhfbStu stu = null;

            Workbook workbook = null;
            Sheet sheet = null;
            Cell cell = null;
            list_stu_w = new ArrayList<JhfbStu>();
            list_stu_m = new ArrayList<JhfbStu>();
            //int class_num = 4;
            //String className = "班级";
          //  boolean sexFlag = true;
            Map<String, List<JhfbStu>> class_map = new HashMap<String, List<JhfbStu>>();
            try {
                workbook = Workbook.getWorkbook(new File(filePath));
                sheet = workbook.getSheet(0);
                int rowCount = sheet.getRows();
                for (int i = 1; i < rowCount; i++) {
                    stu = new JhfbStu();
                    stu = stuInfo(cell,sheet,i,stu);
                    if("1".equals(sexFlag)){
                        //性别开关为打开，需要性别均衡
                        if ("男".equals(stu.getSex())) {
                            list_stu_m.add(stu);
                        } else {
                            list_stu_w.add(stu);
                        }
                    }else{
                        //性别开关关闭，将所有人员放入同一个集合
                        list_stu_m.add(stu);
                    }
                }
                // 排序
                sortList(list_stu_m);
                sortList(list_stu_w);
                // list_count用于遍历学生时候，随机从该集合取一个值，作为对应班级编号，将学生放入对应编号的班级，满足每次循环中学生是随机被放入到各个班级的
                List<Integer> list_count = new ArrayList<Integer>();

                for (int i = 1; i <= class_num; i++) {
                    class_map.put(className+"-" + i, new ArrayList<JhfbStu>());
                    list_count.add(i);
                }

                int random_count = 0;// 随机random_count长度的一个整数
                Random random = new Random();
                ArrayList<JhfbStu> tmp_list = null;
                int classValue = 0;
                for (int j = 0; j < list_stu_m.size(); j++) {
                    // 每次先随机出要放入的学生班级
                    random_count = random.nextInt(list_count.size());
                    classValue = list_count.get(random_count);
                    // 从集合中删除被选中的这个班级
                    list_count.remove(random_count);
                    tmp_list = (ArrayList<JhfbStu>) class_map.get(className+"-"
                            + classValue);
                    tmp_list.add(list_stu_m.get(j).setNewClass(classValue));
                    class_map.put(className+"-" + classValue, tmp_list);

                    if (list_count.size() == 0) {
                        for (int i = 1; i <= class_num; i++) {
                            list_count.add(i);
                        }
                    }
                }
                // 遍历女生
                list_count.clear();
                for (int i = 1; i <= class_num; i++) {
                    list_count.add(i);
                }
                for (int j = 0; j < list_stu_w.size(); j++) {
                    // 每次先随机出要放入的学生班级
                    random_count = random.nextInt(list_count.size());
                    classValue = list_count.get(random_count);
                    // 从集合中删除被选中的这个班级
                    list_count.remove(random_count);
                    tmp_list = (ArrayList<JhfbStu>) class_map.get(className+"-"
                            + classValue);
                    tmp_list.add(list_stu_w.get(j).setNewClass(classValue));
                    class_map.put(className+"-" + classValue, tmp_list);

                    if (list_count.size() == 0) {
                        for (int i = 1; i <= class_num; i++) {
                            list_count.add(i);
                        }
                    }
                }


                // 关闭它，否则会有内存泄露
                workbook.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return class_map;
        }
    /**
     * java对象的集合自定义排序
     *
     * @param list
     * @return
     */
    public static List<JhfbStu> sortList(List<JhfbStu> list) {

        Collections.sort(list, new Comparator<JhfbStu>() {
            @Override
            public int compare(JhfbStu obj1, JhfbStu obj2) {

                if (new BigDecimal(obj1.getAllScore()).compareTo(new BigDecimal(
                        obj2.getAllScore())) > 0) {
                    return -1;
                } else {
                    return 1;
                }
            }
        });
        return list;
    }

    /**
     * 读取学生信息
     * @param cell
     * @param sheet
     * @param i
     * @param stu
     * @return
     */
    public static JhfbStu stuInfo(Cell cell, Sheet sheet, int i, JhfbStu stu){
        cell = sheet.getCell(0, i); // 准考证号
        stu.setTicketNumber(cell.getContents());
        cell = sheet.getCell(1, i); // 姓名
        stu.setStuName(cell.getContents());
        cell = sheet.getCell(2, i); // 性别
        stu.setSex(cell.getContents());
        cell = sheet.getCell(3, i); // 证件号
        stu.setCardNum(cell.getContents());
        cell = sheet.getCell(4, i); // 民族
        stu.setNation(cell.getContents());
        cell = sheet.getCell(5, i); // 报名地州
        stu.setRepCity(cell.getContents());
        cell = sheet.getCell(6, i); // 毕业学校
        stu.setGraduateSchool(cell.getContents());
        cell = sheet.getCell(7, i); // 学籍号
        stu.setStuNumber(cell.getContents());
        cell = sheet.getCell(8, i); // 团员
        stu.setLeagueMembers(cell.getContents());
        cell = sheet.getCell(9, i); // 语文成绩
        stu.setLanguageScore(cell.getContents());
        cell = sheet.getCell(10, i); // 物理化学成绩
        stu.setPhyChemistryScore(cell.getContents());
        cell = sheet.getCell(11, i); // 数学成绩
        stu.setMathScore(cell.getContents());
        cell = sheet.getCell(12, i); // 道德与法制、历史成绩
        stu.setHisScore(cell.getContents());
        cell = sheet.getCell(13, i); // 外语成绩
        stu.setEnglishScore(cell.getContents());
        cell = sheet.getCell(14, i); // 总成绩（不含体育）
        stu.setAllScore(cell.getContents());
        cell = sheet.getCell(15, i); // 体育是否合格
        stu.setSportScore(cell.getContents());
        return stu;
    }
}

